package home.dao;

import home.model.Goods;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class commenDao {
	private SessionFactory sessionFactory;
    public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public List queryForPage(final String hql,final int offset,final int length){
		Session session = null;
		try {
			session = sessionFactory.openSession();
			Query queryObject = session.createQuery(hql);
			queryObject.setFirstResult(offset);
			
			queryObject.setMaxResults(length);
			List<Goods> goodsList = queryObject.list();
			if(goodsList.size() == 0){
				return null;
			}else{
				return goodsList;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			session.close();
		}
    }
    
    public int getAllRowCount(String hql){
		Session session = null;
		try {
			session = sessionFactory.openSession();
			Query queryObject = session.createQuery(hql);
			
			List<Goods> goodsList = queryObject.list();
			return goodsList.size();
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		} finally {
			session.close();
		}
    }
}
